package ru.amse.vlassiev.view;

import java.util.*;

import javax.swing.*;

public class MainWindow extends JFrame {

	private List<JPanel> myFrame;
	private List<JSplitPane> mySplit;
	
	public MainWindow(String title) {
		super(title);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1024, 768);
		myFrame = new LinkedList<JPanel>();
		mySplit = new LinkedList<JSplitPane>();
		JSplitPane firstSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JPanel(), new JPanel());
		firstSplit.setOneTouchExpandable(true);
		this.getContentPane().add(firstSplit);
		mySplit.add(firstSplit);
		this.setVisible(true);
	}
	
	public void addView(JPanel view) {
		myFrame.add(view);
		int n = myFrame.size() - 1;
		if (n == 0) {
			JSplitPane split = mySplit.get(0);
			split.setTopComponent(myFrame.get(0));
		} else if (n == 1) {
			JSplitPane split = mySplit.get(0);
			split.setBottomComponent(myFrame.get(1));
		} else {
			JSplitPane split = mySplit.get(n - 2);
			JSplitPane newSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, myFrame.get(n-1), myFrame.get(n));
			newSplit.setOneTouchExpandable(true);
			split.setBottomComponent(newSplit);
			mySplit.add(newSplit);
		}
		for (JSplitPane split : mySplit) {
			split.setDividerLocation(this.getHeight() / (n + 1));
		}
	}
	
	

}
